"use strict"

var log = console.log


// creates the 'new game' json object
exports.create = function(gametype, playerName) {
	// create some useful vars
	var ctime = new Date().getTime(),
		gameId = gametype + "." + playerName + (Math.random()+"").substr(1)
	
	// generic useful game info
	var myGame = {
		gameId: gameId,
		created: ctime,
		lastUpdated: ctime,
		players: [ playerName ],
		turn: 1,
		turnCount: 0,
		winner: 0
	}
	
	// game specific options
	setupGame(myGame, gametype)
	
	return myGame
}


// specific board info for particular game types
function setupGame(myGame, gametype) {
	var gameopts
	
	switch(gametype) {
		// noughts and crosses
		case "NC":
			gameopts = {
				moveType: "add",
				board: { w: 3, h: 3 },
				state: [
					[0, 0, 0],
					[0, 0, 0],
					[0, 0, 0],
				],
				rules: [
					"checkGameAlreadyWon",
					"checkSquareAlreadyFilled"
				],
				winConditions: [
					"checkFullRow",
					"checkFullColumn",
					"checkFullDiagonals",
					"checkAllSquaresFull"
				]
			}
			break
		
		// noughts and crosses (large)	
		case "NCL":
			gameopts = {
				moveType: "add",
				board: { w: 6, h: 6 },
				state: [
					[0, 0, 0, 0, 0, 0],
					[0, 0, 0, 0, 0, 0],
					[0, 0, 0, 0, 0, 0],
					[0, 0, 0, 0, 0, 0],
					[0, 0, 0, 0, 0, 0],
					[0, 0, 0, 0, 0, 0],
				],
				rules: [
					"checkGameAlreadyWon",
					"checkSquareAlreadyFilled"
				],
				winConditions: [
					"checkRowOf3",
					"checkColumnOf3",
					"checkDiagonalOf3",
					"checkAllSquaresFull"
				]
			}
			break
		
		// connect 4
		case "C4":
			gameopts = {
				moveType: "add",
				board: { w: 7, h: 7 },
				state: [
					[0, 0, 0, 0, 0, 0, 0],
					[0, 0, 0, 0, 0, 0, 0],
					[0, 0, 0, 0, 0, 0, 0],
					[0, 0, 0, 0, 0, 0, 0],
					[0, 0, 0, 0, 0, 0, 0],
					[0, 0, 0, 0, 0, 0, 0],
					[0, 0, 0, 0, 0, 0, 0],
				],
				rules: [
					"checkGameAlreadyWon",
					"checkSquareAlreadyFilled",
					"checkPieceAtLowestLevel"
				],
				winConditions: [
					"checkRowOf4",
					"checkColumnOf4",
					"checkDiagonalOf4",
					"checkAllSquaresFull"
				]
			}
			break
		
		// some kind of strange game I invented which I christened 'The Swapping Game'
		case "SWAP":
			gameopts = {
				moveType: "move",
				board: { w: 5, h: 5 },
				state: [
					[1, 1, 1, 1, 1],
					[0, 0, 0, 0, 0],
					[0, 0, 0, 0, 0],
					[0, 0, 0, 0, 0],
					[2, 2, 2, 2, 2],
				],
				rules: [
					"checkGameAlreadyWon",
					"checkOriginSquareFull",
					"checkOriginSquareContainsPlayersPiece",
					"checkNewDestinationSquareSpecified",
					"checkDestinationSquareEmpty",
					"checkMovedOrthogonally",
					"checkMoveDistanceIs1"
				],
				winConditions: [
					"checkBottomLinePlayer1",
					"checkTopLinePlayer2"
				]
			}
			break
		
		// the 3 musketeers
		case "MUSKET":
			gameopts = {
				moveType: "move",
				board: { w: 5, h: 5 },
				state: [
					[1, 2, 2, 2, 2],
					[2, 2, 2, 2, 2],
					[2, 2, 1, 2, 2],
					[2, 2, 2, 2, 2],
					[2, 2, 2, 2, 1],
				],
				rules: [
					"checkGameAlreadyWon",
					"checkOriginSquareFull",
					"checkOriginSquareContainsPlayersPiece",
					"checkNewDestinationSquareSpecified",
					"checkMovedOrthogonally",
					"checkMoveDistanceIs1",
					
					"player1AndDestinationSquareContainsOpponent",
					"player2AndDestinationSquareEmpty",
				],
				winConditions: [
					"player1InOneRowLose",
					"player1CannotTakeOpponentWin"
				]
			}
			break

		default:
			throw "setupGame error: gametype '" + gametype + "' not found"
	}
	
	// apply the board state to the game board
	for (var gop in gameopts) {
		myGame[gop] = gameopts[gop]
	}
}
